package org.hzero.exam.api.controller.v1;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.*;
import org.hzero.boot.platform.lov.annotation.ProcessLovValue;
import org.hzero.core.base.BaseConstants;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.exam.api.dto.OrderItemDTO;
import org.hzero.exam.api.dto.OrdersDTO;
import org.hzero.exam.app.service.HodrSoHeaderService;
import org.hzero.exam.config.SwaggerTags;
import org.hzero.exam.domain.entity.HodrSoHeader;
import org.hzero.exam.domain.repository.HodrSoHeaderRepository;
import org.hzero.exam.domain.repository.OrderItemRepository;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 马旭阳
 * @date2019/8/5
 * 用于操作订单头信息的controller
 */
@Api(tags = SwaggerTags.HEADER)
@RestController("headerController.v1")
@RequestMapping("/v1/{organizationId}/headers")
public class HodrSoHeaderController extends BaseController {
    @Autowired
    private HodrSoHeaderRepository soHeaderRepository;
    @Autowired
    private OrderItemRepository orderItemRepository;

    @Autowired
    private HodrSoHeaderService soHeaderService;
    /**
     * 根据多条件查询销售订单信息
     * @param organizationId 租户ID
     * @param companyId 公司ID
     * @param customerId 客户ID
     * @param orderNumber 订单编号
     * @param orderStatus 订单状态
     * @param itemId 物料ID
     * @return org.springframework.http.ResponseEntity<java.util.List<org.hzero.exam.domain.entity.HodrSoHeader>>
     */
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "根据多条件分页查询销售订单信息")
    @GetMapping("/more")
    @ProcessLovValue(targetField = BaseConstants.FIELD_BODY)
    public ResponseEntity<Page<HodrSoHeader>> selectByAny(@PathVariable Long organizationId,
                                                          Long  companyId,
                                                          Long  customerId,
                                                          String orderNumber,
                                                          String orderStatus,
                                                          Long itemId,
                                                          PageRequest pageRequest){
        return Results.success(PageHelper.doPage(pageRequest,()->soHeaderRepository.selectByAny(orderNumber,companyId,customerId,orderStatus,itemId)));

    }
    /**
     * 根据订单编号查询订单明细
     * @param organizationId
     * @param orderNumber 订单编号，必填
     * @return org.springframework.http.ResponseEntity<java.util.List<org.hzero.exam.api.dto.OrderItemDTO>>
     */
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "根据订单编号查询订单明细")
    @GetMapping
    @ProcessLovValue(targetField = BaseConstants.FIELD_BODY)
    public ResponseEntity<List<OrderItemDTO>> selectByOrderNumber(@PathVariable Long organizationId,
                                                             @RequestParam String orderNumber
                                                  ){
        return Results.success(orderItemRepository.selectByOrderNumber(orderNumber));
    }
    /**
     * 添加订单头和订单行信息
     * @param organizationId 租户ID
     * @param ordersDTO 存放订单头和订单行信息的DTO类，用于整体添加和修改
     * @return org.springframework.http.ResponseEntity
     */
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "添加订单信息")
    @PostMapping
    public ResponseEntity insert(@PathVariable Long organizationId,
                                                @RequestBody OrdersDTO ordersDTO
                                                ){
        this.validObject(ordersDTO.getHodrSoHeader());
        soHeaderService.insert(ordersDTO);
    return Results.success();
    }
    /**
     * 修改订单头和订单行信息
     * @param organizationId 租户ID
     * @param ordersDTO 存放订单头和订单行信息的DTO类，用于整体添加和修改
     * @return org.springframework.http.ResponseEntity
     */
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "修改订单信息")
    @PutMapping
    public ResponseEntity update(@PathVariable Long organizationId,
                                 @RequestBody OrdersDTO ordersDTO
                                 ){
        this.validObject(ordersDTO.getHodrSoHeader());
        this.validList(ordersDTO.getHodrSoLines());
        SecurityTokenHelper.validToken(ordersDTO.getHodrSoHeader());
        soHeaderService.update(ordersDTO);
        return Results.success();

    }
/**
 * 修改订单头信息
 * @param organizationId
 * @param soHeader 订单头实体类
 * @return org.springframework.http.ResponseEntity
 */
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "修改订单头信息")
    @PutMapping("/orderStatus")
    public ResponseEntity updateByStatusNew(@PathVariable Long organizationId,
                                            @RequestBody HodrSoHeader soHeader){
       this.validObject(soHeader);
       SecurityTokenHelper.validToken(soHeader);
        soHeaderService.updateOne(soHeader);
        return Results.success();
    }
    /**
     * 删除订单信息，有订单行先删除所属订单行，再删除订单头
     * @param organizationId 租户ID
     * @param soHeader 订单头实体类
     * @return org.springframework.http.ResponseEntity
     */
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "删除订单信息")
    @DeleteMapping
    public ResponseEntity delete(@PathVariable Long organizationId,
                                 @RequestBody HodrSoHeader soHeader){
        this.validObject(soHeader);
        SecurityTokenHelper.validToken(soHeader);
        soHeaderService.deleteAll(soHeader);
        return Results.success();
    }
    /**
     * 添加订单头信息
     * @param organizationId 租户ID
     * @param soHeader 订单头实体类
     * @return org.springframework.http.ResponseEntity
     */
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "添加订单头信息")
    @PostMapping("/one")
    public ResponseEntity insertOne(@PathVariable Long organizationId,
                                    @RequestBody HodrSoHeader soHeader){
        this.validObject(soHeader);
        return Results.success(soHeaderRepository.insert(soHeader));
    }
    @Permission(level = ResourceLevel.SITE)
    @ApiOperation(value = "查询订单头信息（测试）")
    @GetMapping("/All")
    public ResponseEntity<List<HodrSoHeader>> selectAll(@PathVariable Long organizationId){
        return Results.success(soHeaderRepository.selectAll());
    }

}
